package com.dc.service.impl;

import com.dc.base.em.ErrorMessageEnum;
import com.dc.base.vo.BaseModel;
import com.dc.base.vo.BusinessException;
import com.dc.dao.LeaseDao;
import com.dc.model.Lease;
import com.dc.service.LeaseService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class LeaseServiceImpl implements LeaseService {
    @Autowired
    LeaseDao leaseDao;
    public BaseModel selectPageLease(Integer curr_page, Integer page_size, BaseModel baseModel,Integer lease_id, String client_name, String flower_name) {
        // 编写业务
        PageHelper.startPage(curr_page,page_size);
        // 开启分页以后需要马上接入查询语句，方便pageHelper实现分页
        List<Lease> users=leaseDao.selectPageLease(lease_id,client_name,flower_name);
        PageInfo pageInfo=new PageInfo(users,5);
        baseModel.setResultCode(0);
        baseModel.setMessage("分页查询租出成功！");
        baseModel.setData(pageInfo);
        return baseModel;
    }
    // 新增租出的接口
    public BaseModel addLease(Lease lease, BaseModel baseModel) throws Exception {
        //调用新增租出的dao
        Integer result=leaseDao.insertLease(lease);
        if(result<=0){
            throw new BusinessException(ErrorMessageEnum.ADD_USER_ERROR);
        }
        baseModel.setResultCode(0);
        baseModel.setMessage("新增租出成功！");
        return baseModel;
    }

    // 修改租出的接口
    public BaseModel updateLease(Lease lease, BaseModel baseModel) throws Exception {
        Integer result=leaseDao.updateLease(lease);
        if(result<=0){
            throw  new BusinessException(1008,"修改租出失败");
        }
        baseModel.setResultCode(0);
        baseModel.setMessage("修改租出成功！");
        return baseModel;
    }

    // 删除租出的接口
    public BaseModel deleteLease(String ids, BaseModel baseModel) throws Exception {
        String[] idArry=ids.split(",");
        int result=leaseDao.deleteLease(idArry);
        if(result<=0){
            throw new BusinessException(1009,"删除"+result+"个租出失败。");
        }
        baseModel.setResultCode(0);
        baseModel.setMessage("删除"+result+"个租出成功！");
        return baseModel;
    }

    // 根据id查询租出信息的接口
    public BaseModel selectLeaseById(Integer lease_id,BaseModel baseModel) throws Exception{
        List<Lease> leaseList = leaseDao.selectLeaseById(lease_id);
        baseModel.setResultCode(0);
        baseModel.setMessage("查询单个租出成功");
        baseModel.setData(leaseList);
        return baseModel;
    }
}
